<?php
/**
 * @特惠赚、会员控制器
 * Author: 小谢     
 * Date: 2019/05/11
 */
namespace app\admin\controller;
use think\facade\Session;
use think\facade\View;
use think\Db;

class User extends Base {
    
    /**
     *  会员列表
     *  @$condition[] = ['','EXP',Db::raw("FIND_IN_SET(".$v['user_id'].",super_ids)")];
     */
    public function lists() {  
        $start_time  = strtotime(I('start_time'));
        $end_time    = strtotime(I('end_time'));
        $keyword     = I('keyword');
        $user_level  = I('user_level');//会员等级

        $where = '';
        if (!empty($start_time) && !empty($end_time)) {
            $where = " u.reg_time >= ".$start_time." AND ". " u.reg_time <=".$end_time;
        }

        if (!empty($where) && $keyword && $user_level >0) {
             $where.= ' AND u.user_id LIKE '.'"%'.$keyword.'%"'. ' OR u.username LIKE '. '"%'.$keyword.'%"'.' OR u.mobile LIKE '. '"%'.$keyword.'%"';
             $where.= ' AND user_level='.$user_level;
        } else if (!empty($where) && $keyword) {
             $where.= ' AND u.user_id LIKE '.'"%'.$keyword.'%"'. ' OR u.username LIKE '. '"%'.$keyword.'%"'.' OR u.mobile LIKE '. '"%'.$keyword.'%"';
        } else if(!empty($where) && $user_level >0) {
             $where.= ' AND user_level='.$user_level;
        } else {
             $where = "1=1";
        }

        $p = I('p/d',1);
        $list = M('user')->alias('u')->join('__USER_INFO__ i','u.user_id = i.user_id')->where($where)->order('u.user_id ASC')->page($p.',20')->paginate(20);

        if ($list) {
            $count = $list->count();
            foreach ($list as $key => $v) {
                $data[] = $v; 
                //推荐人
                $user = M('user')->where(['user_id'=>$v['agentid']])->column('username');
                $data[$key]['invite_name'] = !empty($user[0]) ? $user[0] : '--';
                //节点人数
                $data[$key]['node_count'] = M('user')->where(['agentid'=>$v['user_id']])->count();
                //超级节点人数
                $data[$key]['super_ids']  = M('user')->where('super_ids','like','%'.'|'.$v['user_id'].'|'.'%')->count();
                //已提现金额
                $data[$key]['withdrawals'] = M('withdrawals')->where(['user_id'=>$v['user_id'],'status'=>1])->sum('money');
            }
        }

        View::share([
            'list'=> isset($list) ?  $list : array(),
            "data"=>isset($data) ? $data : array(),
            "stime"=>time(),
            'etime'=>time()+3600*24 *365,
            "count"=>isset($count) ? $count : 0
         ]);
        return View();
    }

    /**
    *@brief 数据列表
    */
    public function show_list() {
        $agent_id = I('get.agentid/i');
    	$user_id  = I('get.user_id');

        //agent_id直推下级会员、super_ids超级节点、整条关系链
        $p = I('p/d',1);
        if(!empty($agent_id)) {
            $list =  M('user')->where(['agentid'=>$agent_id])->order('user_id ASC')->page($p.',20')->paginate(20);
        } else  {
            $list =  M('user')->where('super_ids','like','%'.'|'.$user_id.'|'.'%')->order('user_id ASC')->page($p.',20')->paginate(20); 
        }

        if ($list) {
            foreach ($list as $key => $v) {
               $data[] = $v; 
               //邀请人
               $user = M('user')->where(['user_id'=>$v['agentid']])->column('username');
               $data[$key]['invite_name'] = !empty($user[0]) ? $user[0] : '--';
               //邀请人数
               $data[$key]['invite_number'] =  M('user')->where('super_ids','like','%'.'|'.$v['user_id'].'|'.'%')->count(); 
            }
        }

        $count = $list->count();
        View::share(['count'=>$count,'list'=>$list,"data"=>$data]);
        return View();
    }

    /**
    *@brief 查看会员详细资料
    */
    public function user_show() {
        $user_id = I('get.user_id');
        $info = M('user')->alias('u')->field('u.*,i.*')->join('user_info i','u.user_id = i.user_id')->where(array('u.user_id'=>$user_id))->find();
        View::share(["info"=>$info]);
        return View();          
    }

     /**
     * 删除一条
     */
    public function del(){
        $ids = I('post.ids','');
        empty($ids) &&  $this->ajaxReturn(['status' => -1,'msg' =>"非法操作！",'data'  =>'']);
        DB::name('user')->where('user_id',$ids)->delete();
        $this->ajaxReturn(['status' => 1]);
    }

    /**
    *@brief 批量删除
    */
    public function delAll() {
        $act = I('post.act');
        $ids = I('post.ids');
        if($act == 'delAll') {
             $flag = Db::name("user")->where('user_id','in',$ids)->delete();
             if($flag) {
                $this->ajaxReturn(1);
            } 
            $this->ajaxReturn("删除失败");
        }
    }
    
}